using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MyStack.Models;

namespace MyStack.Controllers
{
	//	This just serves up images for displaying 
    public class ImageController : Controller
    {
        //
        // GET: /Image/

        public ActionResult Index()
        {
            return View();
        }

		/// <summary>
		/// Return an image file to be used in an image tag
		/// Looks up the image based on the user id
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public ActionResult Show(int id)
		{
			MyStackRepository db = new MyStackRepository();

			User user = db.GetUser(id);

			byte[] avatarBytes = null;

			//	If the user didn't porvide an image, use the default
			if (user.Icon == null || user.Icon.Length < 1)
			{
				avatarBytes = System.IO.File.ReadAllBytes(System.AppDomain.CurrentDomain.BaseDirectory + @"\Content\default avatar.jpg");
			}
			//	Otherwise just read the avatar bytes
			else
			{
				avatarBytes = user.Icon.ToArray();
			}

			return File(avatarBytes, "image/jpg");
		}

    }
}
